#!/bin/bash

git stash
git pull origin master --tags
git stash pop

new_version=""
commit_msg=""
repo_name="yt-ios-components"

get_project_name() {
    read -p "enter podspec version: " new_version
    if test -z "$new_version"; then
        get_project_name
    fi
}

get_commit_msg() {
    read -p "enter commit message: " commit_msg
    if test -z "$commit_msg"; then
        get_commit_msg
    fi
}

#get_repo_name() {
#    read -p "enter repo name: " repo_name
#    if test -z "$repo_name"; then
#        get_repo_name
#    fi
#}

cyan='\033[0;36m'
default='\033[0;m'

get_info() {
    get_project_name
    get_commit_msg
#    get_repo_name

    echo -e "\n${default}================================================"
    echo -e "  Version         :  ${cyan}${new_version}${default}"
    echo -e "  Commit message  :  ${cyan}${commit_msg}${default}"
    echo -e "  Repo Name 	   :  ${cyan}${repo_name}${default}"
    echo -e "================================================\n"
}

get_info

echo -e "\n"
confirmed="n"

while [ "$confirmed" != "y" -a "$confirmed" != "Y" ]
do
    read -p "confirm? (y/n):" confirmed
    if [ "$confirmed" == "n" -o "$confirmed" == "N" ]; then
        get_info
    fi
done


version=`grep -E 's.version.*=' FlexLayout.podspec`
line_number=`grep -nE 's.version.*=' FlexLayout.podspec | cut -d : -f1`
sed -i "" "${line_number}s/${version}/ s.version=\"${new_version}\"/g" FlexLayout.podspec

echo "current version is ${version}, new version is ${new_version}"

git add .
git commit -am ${commit_msg}
git push origin master
git tag ${new_version}
git push origin master --tags
cd ~/.cocoapods/repos/${repo_name} && git pull origin master && cd - && pod repo push ${repo_name} FlexLayout.podspec --sources="http://git.missfresh.cn/ios-repo/components.git,https://github.com/CocoaPods/Specs.git,http://git.missfresh.cn/mryt-ios-repo/yt-ios-components.git" --verbose --allow-warnings --use-libraries

